Blog System | Note-6

Blog System | Note-5

@2018年8月1日 14:43:38 @Knowledge From Imooc


####

搜索功能

使用Elasticsearchs实现全文检索

应用@
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 文档类型的EsBlog实体
@Document(indexName = "blog",type = "blog")
public class EsBlog implements Serializable{
@Id
private String id;

// Blog实体的ID
@Field(index = FieldIndex.not_analyzed)
private Long blogId;
private String title;
private String summary;
private String content;
// 不使用全文检索字段
@Field(index = FieldIndex.not_analyzed)
private String username;
...
private String tags;
}

// EsBlogRepository
public interface EsBlogRepository extends ElasticsearchRepository<EsBlog,String> {
// 模糊搜索查询
Page<EsBlog> findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContainingOrTagsContaining(String title,String summary,String content,String tags,Pageable pageable);
}

// EsBlogService
public interface EsBlogService {
// 最新博客列表
Page<EsBlog> listNewestEsBlogs(String keyword, Pageable pageable);
...
}

// Impl
@Override
public List<TagVO> listTop30Tags() {
List<TagVO> list = new ArrayList<>();
// given
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(SearchType.QUERY_THEN_FETCH)
.withIndices("blog").withTypes("blog")
.addAggregation(terms("tags").field("tags").order(Terms.Order.count(false)).size(30))
.build();
// when
Aggregations aggregations = elasticsearchTemplate.query(searchQuery, new ResultsExtractor<Aggregations>() {
@Override
public Aggregations extract(SearchResponse response) {
return response.getAggregations();
}
});

StringTerms modelTerms = (StringTerms)aggregations.asMap().get("tags");
Iterator<Terms.Bucket> modelBucketIt = modelTerms.getBuckets().iterator();
while (modelBucketIt.hasNext()){
Terms.Bucket actiontypeBucket = modelBucketIt.next();
list.add(new TagVO(actiontypeBucket.getKey().toString(),actiontypeBucket.getDocCount()));
}
return list;
}

// BlogController
@Controller
@RequestMapping("/blogs")
public class BlogController {
@Autowired
private EsBlogService esBlogService;

/**
* 博客列表
*/
@GetMapping
public String listBlogs(@RequestParam(value = "order", required = false, defaultValue = "new") String order,
@RequestParam(value = "keyword", required = false, defaultValue = "") String keyword,
@RequestParam(value = "async", required = false) boolean async,
@RequestParam(value = "pageIndex", required = false, defaultValue = "0") int pageIndex,
@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
Model model) {
Page<EsBlog> page = null;
List<EsBlog> list = null;
// 系统初始化时,没有博客数据
boolean isEmpty = true;
try {
// 热门查询
if (order.equals("hot")) {
...
} else if (order.equals("new")) {
...
}
isEmpty = false;
} catch (Exception e) {
...
}
// 所在页面数据列表
list = page.getContent();
model.addAttribute("order", order);
...
// 首次访问页面才加载
if (!async && !isEmpty) {
List<EsBlog> newest = esBlogService.listTop5NewestEsBlogs();
model.addAttribute("newest", newest);
...
}
return (async == true ? "/index :: #mainContainerRepleace" : "/index");
}
}

总结

这一次的博客系统学习,收获颇多(慕课网),

了解到企业级开发的流程(需求分析-技术选型-框架搭建-原型设计-等等)学习到基本的架构设计,

BackEnd:

Spring,Spring MVC,Spring Data,Spring Security,Hibernate,SpringBoot基础知识的巩固;

FrontEnd:

Thymeleaf模版的学习;Bootstrap的使用,jQuery,HTML5,JS,CSS;

Elasticsearchs全文检索;

JpaRepository;

MongoDB-File-Server的使用;

Gradle管理;

继续学习,附下官方文档,保持基础知识的学习,不只是对着代码学着敲而已,官方文档是最好的学习资料。


附录

SpringBoot

Thymeleaf

Bootstrap

Elasticsearch

Gradle

@2018年8月1日 15:02:24